package com.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.beans.Msg;
import com.dao.MsgDao;

public class MsgDaoImpl extends UniversalDaoImpl implements MsgDao {

	public com.vo.Page list(int pageNo, int pageSize, Msg msg) {
		String sql = "select m.id,userid,u.realname,companyid,co.cname companyName,c.name className,classcode,title,description,contact,m.tel,m.email,m.qq,website,inputdate,enddate,sessionid,tag,uptimes,downtimes,m.fix,m.views from j_msg m left join j_code c on m.classCode=c.code and c.type='ms' left join j_user u on u.id=m.userid left join j_company co on co.id=m.companyid where 1=1";
		Map param = new HashMap();
		if (msg.getClassCode() != null && !"".equals(msg.getClassCode()))
			param.put("classcode like ?", msg.getClassCode()+"%");
		if (msg.getTitle() != null && !"".equals(msg.getTitle()))
			param.put("title like ?", "%" + msg.getTitle() + "%");
		if (msg.getDescription() != null && !"".equals(msg.getDescription()))
			param.put("description like ?", "%" + msg.getDescription() + "%");
		if (msg.getUserID() > 0)
			param.put("userid=?", msg.getUserID());
		if (msg.getCompanyID() > 0)
			param.put("companyid=?", msg.getCompanyID());
		return Page(pageNo, pageSize, sql, "inputdate", param);
	}

	public List topFixMsg(int top) {
		String sql = "select top "
				+ top
				+ " m.id,userid,u.realname,companyid,co.cname companyName,c.name className,classcode,title,"
				+ "description,contact,m.tel,m.email,m.qq,website,inputdate,enddate,sessionid,tag,uptimes,"
				+ "downtimes,m.fix,m.views from j_msg m left join j_code c on m.classCode=c.code and c.type='ms' "
				+ "left join j_user u on u.id=m.userid left join j_company co on co.id=m.companyid order by m.fix desc";
		logger.info(sql);
		return getJdbcTemplate().queryForList(sql);
	}

	public List topNewMsg(int top) {
		String sql = "select top "
				+ top
				+ " m.id,userid,u.realname,companyid,co.cname companyName,c.name className,classcode,title,"
				+ "description,contact,m.tel,m.email,m.qq,website,inputdate,enddate,sessionid,tag,uptimes,"
				+ "downtimes,m.fix,m.views from j_msg m left join j_code c on m.classCode=c.code and c.type='ms' "
				+ "left join j_user u on u.id=m.userid left join j_company co on co.id=m.companyid order by m.inputdate desc";
		logger.info(sql);
		return getJdbcTemplate().queryForList(sql);
	}

	public List topViewMsg(int top) {
		String sql = "select top "
				+ top
				+ " m.id,userid,u.realname,companyid,co.cname companyName,c.name className,classcode,title,"
				+ "description,contact,m.tel,m.email,m.qq,website,inputdate,enddate,sessionid,tag,uptimes,"
				+ "downtimes,m.fix,m.views from j_msg m left join j_code c on m.classCode=c.code and c.type='ms' "
				+ "left join j_user u on u.id=m.userid left join j_company co on co.id=m.companyid order by m.views desc";
		logger.info(sql);
		return getJdbcTemplate().queryForList(sql);
	}

	public void fix(int msgID,int fix) {
		String sql="update j_msg set fix="+fix+" where id="+msgID;
		excuteSQL(sql);
	}

	public void addViews(int id) {
		String sql="update j_msg set views=views+1 where id="+id;
		excuteSQL(sql);
	}
}